package com.pt25.service.sys.repository.mysql.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pt25.base.dep.db.DbConstants;
import com.pt25.base.util.CollectionUtil;
import com.pt25.service.sys.repository.mysql.mapper.AdminRolePermissionMapper;
import com.pt25.service.sys.repository.mysql.persistence.AdminRolePermissionRepo;
import com.pt25.service.sys.repository.mysql.po.AdminRolePermissionPo;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AdminRolePermissionRepoImpl extends ServiceImpl<AdminRolePermissionMapper, AdminRolePermissionPo> implements AdminRolePermissionRepo {
    @Override
    public Set<Long> getPermissionIdsByRoleIds(Set<Long> roleIds) {
        if (CollectionUtil.isEmpty(roleIds)) {
            return new HashSet<>();
        }
        QueryWrapper<AdminRolePermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(AdminRolePermissionPo::getRoleId, roleIds);
        queryWrapper.lambda().eq(AdminRolePermissionPo::getDelFlag, DbConstants.DEL_FLAG0);
        List<AdminRolePermissionPo> list = list(queryWrapper);
        return list.stream().map(AdminRolePermissionPo::getPermissionId).collect(Collectors.toSet());
    }

    @Override
    public void delRolePermissionByRoleId(Long roleId) {
        QueryWrapper<AdminRolePermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminRolePermissionPo::getRoleId, roleId);
        remove(queryWrapper);
    }

    @Override
    public void delRolePermissionByPermissionIds(Set<Long> permissionIds) {
        QueryWrapper<AdminRolePermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(AdminRolePermissionPo::getPermissionId, permissionIds);
        remove(queryWrapper);
    }

}
